package org.example.pool;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

public class TestCyclicBarrier {
   public static CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
    public static void main(String[] args) {
        for(int i=1;i<=3;i++){
            Thread t = new Thread(()->{
                try {
                    Thread.sleep(new Random().nextInt(10000));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"来到了餐桌前！");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("人到齐了，开饭了");

                try {
                    Thread.sleep(new Random().nextInt(10000));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"来到了车里面！");
                try {
                    cyclicBarrier.await();
                }catch (Exception e){

                }
                System.out.println("人到齐了，开车了。。。");
            });
            t.start();
        }
    }
}
